package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-10-05
 */
@RestController
@RequestMapping("/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @PostMapping("/save")
    public R save(@RequestBody Patient patient , @RequestHeader("token") String token){
        Long userId = JwtHelper.getUserId(token);
        System.out.println("token = " + token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable Long id){
        patientService.removeById(id);
        return R.ok();
    }

    //修改回显
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id ){
        Patient patient = patientService.selectById(id);
        return R.ok().data("patient",patient);
    }
    //修改
    @PutMapping("/update")
    public R update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }

    @GetMapping("/list")
    public R list(@RequestHeader String token){
        System.out.println("token = " + token);
        Long userId = JwtHelper.getUserId(token);
        List<Patient> list = patientService.selectListByUserId(userId);
        return R.ok().data("list",list);
    }

    @GetMapping("/getPatient/{patientId}")
    public Patient getPatient(@PathVariable("patientId") Long patientId){
        return patientService.getById(patientId);
    }
}

